package cn.estore.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import cn.estore.util.DBConnection;
import cn.estore.entity.ProductEntity;

public class ProductDao {
    private Connection connection = null;
    private DBConnection jdbc = null;
    private PreparedStatement ps = null;


    public ProductDao() {
        jdbc = new DBConnection();
        connection = jdbc.connection;
    }

    /**
     * 查询所有的商品信息
     * @return list 商品列表
     */
    public List<ProductEntity> selectAllProducts() {
        List<ProductEntity> list = new ArrayList<>();

        try {
            ps = connection.prepareStatement("select  * from tb_product order by id DESC ");
            ResultSet rs = ps.executeQuery();
            list = resultSetToList(rs);
        } catch (SQLException ex) {
            System.out.println("数据库访问失败");
        }

        return list;
    }


    //根据discount字段查询商品信息
    public List<ProductEntity> selectProductsByDiscount(int discount) {
        List<ProductEntity> list = new ArrayList<>();
        try {
            ps = connection.prepareStatement("select * from tb_product where discount=? order by id DESC");
            ps.setInt(1, discount);
            ResultSet rs = ps.executeQuery();
            list = resultSetToList(rs);
        } catch (SQLException ex) {
            System.out.println("数据库访问失败");
        }
        return list;
    }

    //将ResultSet类型的数据转换为List，其中元素类型为ProductEntity
    public List<ProductEntity> resultSetToList(ResultSet rs) throws SQLException {
        List<ProductEntity> list = new ArrayList<>();
        while (rs.next()) {
            ProductEntity e = new ProductEntity();
            e.setId(rs.getInt("id"));
            e.setCategoryMainId(rs.getInt("category_main_id"));
            e.setCategoryBranchId(rs.getInt("category_branch_id"));
            e.setName(rs.getString("name"));
            e.setProducingArea(rs.getString("producing_area"));
            e.setDescription(rs.getString("description"));
            e.setCreateTime(rs.getString("create_time"));
            e.setMarketPrice(rs.getFloat("market_price"));
            e.setSellPrice(rs.getFloat("sell_price"));
            e.setProductAmount(rs.getInt("product_amount"));
            e.setPicture(rs.getString("picture"));
            e.setDiscount(rs.getInt("discount"));
            list.add(e);
        }
        return list;
    }

    //以商品的名称为搜索条件查询所有商品
    public List<ProductEntity> selectProductSearch(String name) {
        List<ProductEntity> list = new ArrayList<>();
        try {
            // 创建一个PrepareStatement对象，用于执行SQL语句
            ps = connection.prepareStatement("select * from tb_product where name like ? order by id DESC");
            // 设置查询参数
            ps.setString(1, "%" + name + "%");
            // 执行查询操作
            ResultSet rs = ps.executeQuery();
            // 将查询结果转换为List集合
            list = resultSetToList(rs);
        } catch (SQLException ex) {
            System.out.println("数据库访问失败");
        }
        return list;
    }

}
